package com.glp.work.flow.controller;

import com.glp.common.reponse.BaseResponse;
import com.glp.common.reponse.BaseResult;
import com.glp.fbs.api.dto.user.PersonInfoDto;
import com.glp.work.flow.constant.APICode;
import com.glp.work.flow.constant.Keys;
import com.glp.work.flow.constant.RiskLevelEnum;
import com.glp.work.flow.dto.AppInfoResp;
import com.glp.work.flow.dto.request.PersonInfoReq;
import com.glp.work.flow.dto.request.RiskLevelQueryRequest;
import com.glp.work.flow.dto.request.RoleAndPermReq;
import com.glp.work.flow.dto.response.RiskLevelQueryResponse;
import com.glp.work.flow.service.client.PermissionClient;
import com.glp.work.flow.service.UserService;
import com.glp.work.flow.utils.TraceIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户的信息以及状态
 *
 * @author liwenhong
 */
@RestController
@Slf4j
@RequestMapping("permission")
@Api(tags = "权限相关")
public class PermissionController {

    @Resource
    private PermissionClient permissionService;

    @Resource
    private UserService userService;

    @Value("${glp.fbs-app.appId}")
    private String appId;

    @Value("${glp.login.html}")
    private String loginHtml;

    @ApiOperation("个人角色，菜单资源列表")
    @GetMapping("/roleAndPermList")
    public BaseResult userRoleAndPermList(@RequestHeader(value = Keys.USERNO) String userNo) {
        return permissionService.getRoleAndPermList(userNo, appId);
    }

    @ApiOperation("个人资料")
    @GetMapping("/personInfo")
    public BaseResult userPersonInfo(@RequestHeader(value = Keys.USERNO) String userNo) {
        return userService.getPersonInfo(userNo);
    }

    @ApiOperation("多人资料")
    @PostMapping("/personInfos")
    public BaseResult<List<PersonInfoDto>> userPersonInfos(@RequestBody PersonInfoReq req) {
        return new BaseResult<>(APICode.SUCCESS.getCode(), "获取多人资料" + TraceIDUtils.getLogTraceId(), userService.getPersonInfos(req));
    }

    @ApiOperation("用户拥有应用列表")
    @GetMapping("/appList")
    public BaseResult userAppList(@RequestHeader(value = Keys.USERNO) String userNo) {
        return permissionService.getAppList(userNo);
    }

    @ApiOperation("查询用户的角色与资源ID")
    @GetMapping("/getRoleIdsAndPermIds")
    public BaseResult userRoleIdsAndPermIds(@RequestHeader(value = Keys.USERNO) String userNo) {
        return permissionService.getRoleIdsAndPermIds(userNo);
    }

    @ApiOperation("查询多个角色的资源ID")
    @PostMapping("/getPermIdsByRoleIds")
    public BaseResult getPermIdsByRoleIds(@RequestBody RoleAndPermReq req) {
        return permissionService.getPermIdsByRoleIds(req);
    }

    @ApiOperation("查询当前用户下级列表")
    @GetMapping("/getSubPersonInfos")
    public BaseResult getSubPersonInfos(@RequestHeader(value = Keys.USERNO) String userNo) {
        return permissionService.getSubPersonInfos(userNo);
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    public BaseResult logout(@RequestHeader(value = Keys.token) String token) {
        permissionService.logout(token, appId);
        return BaseResponse.success(loginHtml);
    }

    @ApiOperation("获取平台详情")
    @GetMapping("/detailByAppId")
    public AppInfoResp detailByAppId(@RequestHeader String appId) {
        return userService.detailByAppId(appId);
    }

    @ApiOperation("获取用户风险等级")
    @PostMapping("/batchRiskLevel")
    public BaseResult<List<RiskLevelQueryResponse>> batchRiskLevel(@RequestBody RiskLevelQueryRequest request) {
        List<RiskLevelQueryResponse> riskLevelQueryResponses = userService.batchRiskLevel(request);
        if (!CollectionUtils.isEmpty(riskLevelQueryResponses)) {
            riskLevelQueryResponses.forEach(riskLevelQueryResponse -> {
                if (riskLevelQueryResponse != null && riskLevelQueryResponse.getPersonalRiskLevel() != null) {
                    int i = Integer.parseInt(riskLevelQueryResponse.getCommonRiskLevel());
                    int j = Integer.parseInt(riskLevelQueryResponse.getPersonalRiskLevel());
                    if (i > j) {
                        riskLevelQueryResponse.setRiskLevel(RiskLevelEnum.getEnum(riskLevelQueryResponse.getCommonRiskLevel()).getRemark());
                    } else {
                        riskLevelQueryResponse.setRiskLevel(RiskLevelEnum.getEnum(riskLevelQueryResponse.getPersonalRiskLevel()).getRemark());
                    }
                }
            });
        }
        return new BaseResult<>(APICode.SUCCESS.getCode(), "获取用户风险等级" + TraceIDUtils.getLogTraceId(), riskLevelQueryResponses);
    }


}
